package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import dicts.PaymentDict;
import dicts.RealmDict;
import entities.TMb;
import entities.TMerchant;
import interfaces.IMbService;
import interfaces.IMerchantService;
import net.sf.json.JSONObject;
import util.HttpUtil;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class MbAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IMerchantService merchantService;
	private IMbService mbService;

	private TMerchant merchantBean;
	private List<TMerchant> merchantList;
	private TMb mbBean;
	private List<TMb> mbList;

	private String mbNo;
	private String mbNos;
	private String merNo;
	private String mbType;
	private String mbBank;
	private String mbBranch;
	private String mbBanker;
	private String mbAccount;
	private Double mbAmount;
	private String mbBankz;
	private String mbBranchz;
	private String mbBankerz;
	private String mbAccountz;
	private String mbState;
	private Date mbDate;
	private Date mbDateTop;
	private Date mbDateEnd;
	private String mbVstate;
	private Date mbVdate;
	private Date mbVdateTop;
	private Date mbVdateEnd;
	private String mbRemark;

	// 展示对象
	public String show() throws Exception {
		if (!mbService.exist(mbNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			mbBean = mbService.fetch(mbNo);
			return SUCCESS;
		}
	}
	
	// 展示对象
	public String page() throws Exception {
		if (!mbService.exist(mbNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			mbBean = mbService.fetch(mbNo);
			merchantBean=merchantService.fetch(mbBean.getMerNo());
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!mbService.exist(mbNo)) {
			mbBean = new TMb();
		} else {
			mbBean = mbService.fetch(mbNo);
		}
		return SUCCESS;
	}
	

	// 编辑对象
	public String editpenalty() throws Exception {
		return SUCCESS;
	}

	public void saveverify() throws Exception {
		try {
			for (String mbNo : StringUtil.split(mbNos)) {
				JSONObject resultMap = new JSONObject();
				Map<String, String> paramsMap = new HashMap<String, String>();
				paramsMap.put("mbNo", mbNo);
				paramsMap.put("mbState", mbState);
				paramsMap.put("mbRemark", mbRemark);
				resultMap = JSONObject.fromObject(new HttpUtil().post(RealmDict.GATEWAY + "Verify", paramsMap));
				addActionError("返回码[" + resultMap.getString("code") + "]返回信息[" + resultMap.getString("message") + "]");
			}
			ServletActionContext.getResponse().getWriter().print("succ");
		} catch (Exception e) {
			ServletActionContext.getResponse().getWriter().print("fail");
		}
	}
	
	public String savepenalty() throws Exception {
		JSONObject resultMap = new JSONObject();
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("merNo", merNo);
		paramsMap.put("mbAmount", NumberUtil.transfer(mbAmount));
		paramsMap.put("mbRemark", mbRemark);
		resultMap = JSONObject.fromObject(new HttpUtil().post(RealmDict.GATEWAY + "Penalty", paramsMap));
		addActionError("返回码["+resultMap.getString("code")+"]返回信息["+resultMap.getString("message")+"]");
		return SUCCESS;
	}

	public String listsettle() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mbNo", StringUtil.split(mbNo));
		paramMap.put("merNo", merNo);
		paramMap.put("mbType", PaymentDict.MB_SETTLE);
		paramMap.put("mbBank", mbBank);
		paramMap.put("mbBranch", mbBranch);
		paramMap.put("mbBanker", mbBanker);
		paramMap.put("mbAccount", mbAccount);
		paramMap.put("mbAmount", mbAmount);
		paramMap.put("mbBankz", mbBankz);
		paramMap.put("mbBranchz", mbBranchz);
		paramMap.put("mbBankerz", mbBankerz);
		paramMap.put("mbAccountz", mbAccountz);
		paramMap.put("mbState", mbState);
		paramMap.put("mbDate", ParameterUtil.dateCombin(mbDateTop, mbDateEnd));
		paramMap.put("mbVstate", mbVstate);
		paramMap.put("mbVdate", ParameterUtil.dateCombin(mbVdateTop, mbVdateEnd));
		paramMap.put("mbRemark", mbRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mbDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMb> resultSet = mbService.search(querySet);
		mbList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	public String listverify() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mbNo", StringUtil.split(mbNo));
		paramMap.put("merNo", merNo);
		paramMap.put("mbType", PaymentDict.MB_SETTLE);
		paramMap.put("mbBank", mbBank);
		paramMap.put("mbBranch", mbBranch);
		paramMap.put("mbBanker", mbBanker);
		paramMap.put("mbAccount", mbAccount);
		paramMap.put("mbAmount", mbAmount);
		paramMap.put("mbBankz", mbBankz);
		paramMap.put("mbBranchz", mbBranchz);
		paramMap.put("mbBankerz", mbBankerz);
		paramMap.put("mbAccountz", mbAccountz);
		paramMap.put("mbState", mbState);
		paramMap.put("mbDate", ParameterUtil.dateCombin(mbDateTop, mbDateEnd));
		paramMap.put("mbVstate", mbVstate);
		paramMap.put("mbVdate", ParameterUtil.dateCombin(mbVdateTop, mbVdateEnd));
		paramMap.put("mbRemark", mbRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mbDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMb> resultSet = mbService.search(querySet);
		mbList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}
	
	public String listpenalty() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mbNo", StringUtil.split(mbNo));
		paramMap.put("merNo", merNo);
		paramMap.put("mbType", PaymentDict.MB_PENALTY);
		paramMap.put("mbBank", mbBank);
		paramMap.put("mbBranch", mbBranch);
		paramMap.put("mbBanker", mbBanker);
		paramMap.put("mbAccount", mbAccount);
		paramMap.put("mbAmount", mbAmount);
		paramMap.put("mbBankz", mbBankz);
		paramMap.put("mbBranchz", mbBranchz);
		paramMap.put("mbBankerz", mbBankerz);
		paramMap.put("mbAccountz", mbAccountz);
		paramMap.put("mbState", mbState);
		paramMap.put("mbDate", ParameterUtil.dateCombin(mbDateTop, mbDateEnd));
		paramMap.put("mbVstate", mbVstate);
		paramMap.put("mbVdate", ParameterUtil.dateCombin(mbVdateTop, mbVdateEnd));
		paramMap.put("mbRemark", mbRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mbDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMb> resultSet = mbService.search(querySet);
		mbList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String exportsettle() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("mbNo", StringUtil.split(mbNo));
		paramMap.put("merNo", merNo);
		paramMap.put("mbType", PaymentDict.MB_SETTLE);
		paramMap.put("mbBank", mbBank);
		paramMap.put("mbBranch", mbBranch);
		paramMap.put("mbBanker", mbBanker);
		paramMap.put("mbAccount", mbAccount);
		paramMap.put("mbAmount", mbAmount);
		paramMap.put("mbBankz", mbBankz);
		paramMap.put("mbBranchz", mbBranchz);
		paramMap.put("mbBankerz", mbBankerz);
		paramMap.put("mbAccountz", mbAccountz);
		paramMap.put("mbState", mbState);
		paramMap.put("mbDate", ParameterUtil.dateCombin(mbDateTop, mbDateEnd));
		paramMap.put("mbVstate", mbVstate);
		paramMap.put("mbVdate", ParameterUtil.dateCombin(mbVdateTop, mbVdateEnd));
		paramMap.put("mbRemark", mbRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "mbDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMb> resultSet = mbService.search(querySet);
		mbList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("商户编号");
		fieldHead.add("结算类型");
		fieldHead.add("结算银行名称");
		fieldHead.add("支行");
		fieldHead.add("结算银行用户名");
		fieldHead.add("结算银行帐号");
		fieldHead.add("金额");
		fieldHead.add("汇款银行");
		fieldHead.add("汇款支行");
		fieldHead.add("汇款账户");
		fieldHead.add("汇款账户名");
		fieldHead.add("提现状态");
		fieldHead.add("发生日期日期");
		fieldHead.add("发生日期时间");
		fieldHead.add("审核状态");
		fieldHead.add("审核日期日期");
		fieldHead.add("审核日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TMb mbBean : mbList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(mbBean.getMbNo());
			fieldItem.add(mbBean.getMerNo());
			fieldItem.add(mbBean.getMbType());
			fieldItem.add(mbBean.getMbBank());
			fieldItem.add(mbBean.getMbBranch());
			fieldItem.add(mbBean.getMbBanker());
			fieldItem.add(mbBean.getMbAccount());
			fieldItem.add(mbBean.getMbAmount().toString());
			fieldItem.add(mbBean.getMbBankz());
			fieldItem.add(mbBean.getMbBranchz());
			fieldItem.add(mbBean.getMbBankerz());
			fieldItem.add(mbBean.getMbAccountz());
			fieldItem.add((mbBean.getMbState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(mbBean.getMbDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(mbBean.getMbDate(), DateUtil.TIME));
			fieldItem.add((mbBean.getMbVstate().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(mbBean.getMbVdate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(mbBean.getMbVdate(), DateUtil.TIME));
			fieldItem.add(mbBean.getMbRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IMbService getMbService() {
		return mbService;
	}

	public void setMbService(IMbService mbService) {
		this.mbService = mbService;
	}

	public TMb getMbBean() {
		return mbBean;
	}

	public void setMbBean(TMb mbBean) {
		this.mbBean = mbBean;
	}

	public List<TMb> getMbList() {
		return mbList;
	}

	public void setMbList(List<TMb> mbList) {
		this.mbList = mbList;
	}

	public String getMbNo() {
		return mbNo;
	}

	public void setMbNo(String mbNo) {
		this.mbNo = mbNo;
	}

	public String getMbNos() {
		return mbNos;
	}

	public void setMbNos(String mbNos) {
		this.mbNos = mbNos;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public String getMbType() {
		return mbType;
	}

	public void setMbType(String mbType) {
		this.mbType = mbType;
	}

	public String getMbBank() {
		return mbBank;
	}

	public void setMbBank(String mbBank) {
		this.mbBank = mbBank;
	}

	public String getMbBranch() {
		return mbBranch;
	}

	public void setMbBranch(String mbBranch) {
		this.mbBranch = mbBranch;
	}

	public String getMbBanker() {
		return mbBanker;
	}

	public void setMbBanker(String mbBanker) {
		this.mbBanker = mbBanker;
	}

	public String getMbAccount() {
		return mbAccount;
	}

	public void setMbAccount(String mbAccount) {
		this.mbAccount = mbAccount;
	}

	public Double getMbAmount() {
		return mbAmount;
	}

	public void setMbAmount(Double mbAmount) {
		this.mbAmount = mbAmount;
	}

	public String getMbBankz() {
		return mbBankz;
	}

	public void setMbBankz(String mbBankz) {
		this.mbBankz = mbBankz;
	}

	public String getMbBranchz() {
		return mbBranchz;
	}

	public void setMbBranchz(String mbBranchz) {
		this.mbBranchz = mbBranchz;
	}

	public String getMbBankerz() {
		return mbBankerz;
	}

	public void setMbBankerz(String mbBankerz) {
		this.mbBankerz = mbBankerz;
	}

	public String getMbAccountz() {
		return mbAccountz;
	}

	public void setMbAccountz(String mbAccountz) {
		this.mbAccountz = mbAccountz;
	}

	public String getMbState() {
		return mbState;
	}

	public void setMbState(String mbState) {
		this.mbState = mbState;
	}

	public Date getMbDate() {
		return mbDate;
	}

	public void setMbDate(Date mbDate) {
		this.mbDate = mbDate;
	}

	public Date getMbDateTop() {
		return mbDateTop;
	}

	public void setMbDateTop(Date mbDateTop) {
		this.mbDateTop = mbDateTop;
	}

	public Date getMbDateEnd() {
		return mbDateEnd;
	}

	public void setMbDateEnd(Date mbDateEnd) {
		this.mbDateEnd = mbDateEnd;
	}

	public String getMbVstate() {
		return mbVstate;
	}

	public void setMbVstate(String mbVstate) {
		this.mbVstate = mbVstate;
	}

	public Date getMbVdate() {
		return mbVdate;
	}

	public void setMbVdate(Date mbVdate) {
		this.mbVdate = mbVdate;
	}

	public Date getMbVdateTop() {
		return mbVdateTop;
	}

	public void setMbVdateTop(Date mbVdateTop) {
		this.mbVdateTop = mbVdateTop;
	}

	public Date getMbVdateEnd() {
		return mbVdateEnd;
	}

	public void setMbVdateEnd(Date mbVdateEnd) {
		this.mbVdateEnd = mbVdateEnd;
	}

	public String getMbRemark() {
		return mbRemark;
	}

	public void setMbRemark(String mbRemark) {
		this.mbRemark = mbRemark;
	}

	public IMerchantService getMerchantService() {
		return merchantService;
	}

	public void setMerchantService(IMerchantService merchantService) {
		this.merchantService = merchantService;
	}

	public TMerchant getMerchantBean() {
		return merchantBean;
	}

	public void setMerchantBean(TMerchant merchantBean) {
		this.merchantBean = merchantBean;
	}

	public List<TMerchant> getMerchantList() {
		return merchantList;
	}

	public void setMerchantList(List<TMerchant> merchantList) {
		this.merchantList = merchantList;
	}

}
